package com.shinow.capacitor.plusins.speedata;

import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.shinow.capacitor.plusins.speedata.sd60id.Sd60id;

import java.io.IOException;

@CapacitorPlugin(name = "Speedata")
public class SpeedataPlugin extends Plugin {
	private Sd60id sd60idImplementation = new Sd60id();

    @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
    public void initSd60Id(PluginCall call) {
        call.setKeepAlive(true);
        sd60idImplementation.initSd60Id(call, getActivity());
    }

    @PluginMethod
    public void getSd60IdInfor(PluginCall call) {
        boolean Loop = call.getBoolean("Loop", false);
        int Interval = call.getInt("Interval", 600);

        try {
            sd60idImplementation.getSd60IdInfor(Loop, Interval);
            call.resolve();
        } catch (SpeeDataRunTimeException e) {
            call.reject(e.getMessage());
        }
    }

    @PluginMethod(returnType = PluginMethod.RETURN_NONE)
    public void stopGetSd60IdInfor(PluginCall call) {
        sd60idImplementation.stopGetSd60IdInfor();
        call.resolve();
    }

    @PluginMethod
    public void releaseSd60Id(PluginCall call) {
        try {
            sd60idImplementation.releaseSd60Id();
            call.resolve();
        } catch (IOException e) {
            call.reject("设备释放失败");
        } catch (SpeeDataRunTimeException e) {
            call.reject(e.getMessage());
        }
    }
}
